<?php
/**
 * Created by xiaokengge
 * User: zhaoyu
 * Date: 2023/12/26
 * Time: 22:37
 *describe:****
 */

namespace app\common\service;


use fast\Tree;

class ProductService
{
    public function productCategoryTree($where = '')
    {
        $list = (new \app\admin\model\product\Category())->where($where)->where($where)->select();

        $objList = []; $ids=[];
        foreach ($list as $k => $v) {
            $childrenList = Tree::instance()->init($list, 'pid')->getChildren($v['id'], true);
            $obj = Tree::instance()->init($childrenList, 'pid')->getTreeArray($v['pid']);

//                $objList[]=$obj;
            if(!in_array($obj[0]->id,$ids)){
                $rr=  Tree::instance()->getTreeListNew($obj);

                if($rr){
                    foreach ($rr as $ke=>$v1){
                        $objList[]=$v1;
                        $ids[]= $v1['id'];
                    }

                }
            }
//            array_push($objList,$rr);
        }
        if (!empty($objList)) {
            $objList = collection($objList)->toArray();
        }
        if (!$where) {
            array_unshift($objList, ['id' => 0, "pid" => 0, 'name' => '顶级分类', 'haschild' => 0]);
        }

        $returnData = [
            'rows' => $objList,
            'total' => count($list)
        ];
        return $returnData;
    }

    public function envCategoryTree($where = '')
    {
        $list = (new \app\admin\model\Env\Category())->where($where)->where($where)->select();

        $objList = []; $ids=[];
        foreach ($list as $k => $v) {
            $childrenList = Tree::instance()->init($list, 'pid')->getChildren($v['id'], true);
            $obj = Tree::instance()->init($childrenList, 'pid')->getTreeArray($v['pid']);

//                $objList[]=$obj;
            if(!in_array($obj[0]->id,$ids)){
                $rr=  Tree::instance()->getTreeListNew($obj);

                if($rr){
                    foreach ($rr as $ke=>$v1){
                        $objList[]=$v1;
                        $ids[]= $v1['id'];
                    }

                }
            }
//            array_push($objList,$rr);
        }
        if (!empty($objList)) {
            $objList = collection($objList)->toArray();
        }

        if (!$where) {
            array_unshift($objList, ['id' => 0, "pid" => 0, 'name' => '顶级分类', 'haschild' => 0]);
        }

        $returnData = [
            'rows' => $objList,
            'total' => count($list)
        ];
        return $returnData;
    }
}
